import { app } from 'electron';

import path from 'path';

export default class MainCommonService {
  static get isDevEnv(): boolean {
    return process.env.npm_lifecycle_event === 'app:dev';
  }

  /**
   * Gets current app root path.
   */
  static getRootPath(): string {
    // app.getAppPath() returns project root path in dev.
    // app.getAppPath() returns app-root-path/resources/app.asar in release.
    // That's why extra work is needed to resolve the path.
    let rootPath = app.getAppPath();

    if (rootPath.toLowerCase().endsWith('app.asar')) {
      rootPath = path.resolve(rootPath, '..');
    }

    if (rootPath.toLowerCase().endsWith('resources')) {
      rootPath = path.resolve(rootPath, '..');
    }

    return rootPath;
  }
}
